package com.zrf.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.zrf.hibernate.pojo.Seller;


@Repository("sellerDao")
public class SellerDaoImpl extends BaseDaoImpl<Seller>{

	private Session session = null;
	private Query query = null;
	//卖家登录
	public boolean login(String name,String password){
		boolean result = false;
		String hql = "FROM Seller s WHERE s.sname=? AND s.spassword=?";
		
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			query.setString(0, name);
			query.setString(1, password);
			if(query.list()!=null && query.list().size()==1){
				result = true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
		return result;
	}
	
	
	public Seller getSellerByName(String name){
		List<?> list  = null;
		String hql = "FROM Seller s WHERE s.sname=?";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			query.setString(0, name);
			list = query.list();
			if(list!=null && list.size()<1){
				list = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		if(session!=null && session.isOpen()){
			session.close();
		}
		return (Seller) list.get(0);
	}
	
	
	
}
